﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using EM.Repository.utility;
using System.Web.UI.HtmlControls;

namespace AlternateProject
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        public string APPROOT = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.CreateMenu();

            AppPathFinder finder = new AppPathFinder();
            APPROOT = finder.GetApplicationPath();
        }


        Authentication ua = new Authentication();
        private void CreateMenu()
        {
            if (ua.hasSessionAuthentication())
            {
                SqlCommand cmd = new SqlCommand();
                DbHelper dbhelper = new DbHelper();
                string sSQL = "";
                sSQL += " SELECT P.PAGEID, MENUNAME, PARENTPAGEID, PAGETYPE, NULL AS PAGEURL ";
                sSQL += " FROM TBLPAGE P, TBLPAGE_L L ";
                sSQL += " WHERE P.PAGEID = L.PAGEID ";
                sSQL += " AND STATUS = 'A' ";
                sSQL += " AND L.LANGCODE = @LANGCODE ";
                sSQL += " AND GETDATE() BETWEEN STARTDATE AND DATEADD(\"d\", 1, ISNULL(ENDDATE,'31/12/2099')) ";
                sSQL += " AND DISPLAYMENUFLG = 'Y'  ";
                sSQL += " AND PAGETYPE = 'M' ";
                if (!ua.UserIsAnAdmin(Session["USERID"].ToString()))
                {
                    sSQL += " AND P.PAGEID IN (SELECT PAGEID FROM TBLROLEPAGE_COMPILED WHERE ROLEID IN (	SELECT UR.ROLEID FROM TBLUSERROLE UR, TBLROLE R WHERE USERID = @USERID AND UR.ROLEID = R.ROLEID AND R.STATUS = 'A' AND GETDATE() BETWEEN UR.STARTDATE AND ISNULL(UR.ENDDATE,'01/01/2099') AND GETDATE() BETWEEN R.STARTDATE AND ISNULL(R.ENDDATE,'01/01/2099'))) ";
                    /*BU KONTROL ÇOK KRİTİK. */
                    sSQL += " AND DBO.fnGetPageChildItemCount(P.PAGEID, @USERID, GETDATE()) > 0";
                }
                sSQL += " ORDER BY PAGEORDER ";
                cmd.CommandText = sSQL;
                cmd.Parameters.Add("LANGCODE", SqlDbType.NVarChar).Value = Session["LANGCODE"].ToString();
                cmd.Parameters.Add("USERID", SqlDbType.Int).Value = Convert.ToInt32(Session["USERID"].ToString());

                DataTable dt = dbhelper.GetDataTable(cmd, "");

                rptMainMenu.DataSource = dt;
                rptMainMenu.DataBind();

                if (Session["MainPageId"] != null)
                    this.CreateSubMenu(Convert.ToInt32(Session["MainPageId"]));
            }
        }

        protected void rptSubMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HtmlAnchor aMenu = e.Item.FindControl("aMenu") as HtmlAnchor;
                DataRowView row = e.Item.DataItem as DataRowView;

                aMenu.HRef = row["PAGEURL"].ToString();

                if (Request.QueryString["pgid"] != null)
                {
                    if (row["PAGEID"].ToString() == Request.QueryString["pgid"].ToString())
                        aMenu.Attributes.Add("id", "active");
                }
            }
        }

        protected void rptMainMenu_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "GetSubMenu")
            {
                CreateSubMenu(Convert.ToInt32(e.CommandArgument));
            }
        }

        private void CreateSubMenu(int PageId)
        {
            SqlCommand cmd = new SqlCommand();
            string sSQL = @"
                SELECT P.PAGEID, MENUNAME, NULL PARENTPAGEID, PAGETYPE, ";
            sSQL += @" CASE WHEN PAGEURL IS NULL THEN '' ELSE '" + APPROOT + "/' + PAGEURL + '?pgid=' + convert(nvarchar,P.PAGEID) END AS PAGEURL, PAGEORDER ";
            sSQL += @"FROM TBLPAGE P, TBLPAGE_L L  
                WHERE P.PAGEID = L.PAGEID  
                AND STATUS = 'A'  
                AND L.LANGCODE = @LANGCODE
                AND GETDATE() BETWEEN STARTDATE AND DATEADD(d, 1, ISNULL(ENDDATE,'31/12/2099')) AND DISPLAYMENUFLG = 'Y'   
                AND PARENTPAGEID = @PAGEID ";

            if (!ua.UserIsAnAdmin(Session["USERID"].ToString()))
            {
                sSQL += @" AND P.PAGEID IN (SELECT PAGEID FROM TBLROLEPAGE_COMPILED WHERE ROLEID IN (SELECT ROLEID FROM TBLUSERROLE WHERE USERID = @USERID AND GETDATE() BETWEEN STARTDATE AND DATEADD(d,1, ISNULL(ENDDATE,CONVERT(DATETIME,'30/12/2099'))))) 
                                AND DBO.fnGetPageChildItemCount(P.PAGEID, @USERID, GETDATE()) > 0";
            }
            sSQL += @" UNION ALL 
                SELECT P.PAGEID, MENUNAME, PARENTPAGEID, PAGETYPE, ";
            sSQL += " CASE WHEN PAGEURL IS NULL THEN '' ELSE '" + APPROOT + "/' + PAGEURL + '?pgid=' + convert(nvarchar,P.PAGEID) END AS PAGEURL, PAGEORDER ";
            sSQL += @" FROM TBLPAGE P, TBLPAGE_L L  
                WHERE P.PAGEID = L.PAGEID  
                AND STATUS = 'A'  
                AND L.LANGCODE = @LANGCODE
                AND GETDATE() BETWEEN STARTDATE AND DATEADD(d, 1, ISNULL(ENDDATE,'31/12/2099'))  
                AND DISPLAYMENUFLG = 'Y'   ";

            if (!ua.UserIsAnAdmin(Session["USERID"].ToString()))
            {
                sSQL += " AND P.PAGEID IN (SELECT PAGEID FROM TBLROLEPAGE_COMPILED WHERE ROLEID IN (SELECT ROLEID FROM TBLUSERROLE WHERE USERID = @USERID AND GETDATE() BETWEEN STARTDATE AND DATEADD(d,1, ISNULL(ENDDATE,CONVERT(DATETIME,'30/12/2099'))))) ";
            }

            sSQL += @" AND PARENTPAGEID IN ( 
                 		SELECT P.PAGEID 
                 		FROM TBLPAGE P 
                 		WHERE 1 = 1 
                 		AND STATUS = 'A'
                 		AND GETDATE() BETWEEN STARTDATE AND DATEADD(d, 1, ISNULL(ENDDATE,'31/12/2099'))  
                 		AND DISPLAYMENUFLG = 'Y'   
                 		AND PARENTPAGEID =  @PAGEID ";
            if (!ua.UserIsAnAdmin(Session["USERID"].ToString()))
            {
                sSQL += " 		AND P.PAGEID IN (SELECT PAGEID FROM TBLROLEPAGE_COMPILED WHERE ROLEID IN (SELECT ROLEID FROM TBLUSERROLE WHERE USERID = @USERID AND GETDATE() BETWEEN STARTDATE AND DATEADD(d,1, ISNULL(ENDDATE,CONVERT(DATETIME,'30/12/2099'))))) ";
            }
            sSQL += @" ) 
                        ORDER BY PAGEORDER";

            DataTable dtSubMenu = new DataTable();
            cmd = new SqlCommand();
            cmd.CommandText = sSQL;
            cmd.Parameters.Add("@LANGCODE", SqlDbType.NVarChar).Value = Session["LANGCODE"].ToString();
            cmd.Parameters.Add("@USERID", SqlDbType.Int).Value = Convert.ToInt32(Session["USERID"].ToString());
            cmd.Parameters.AddWithValue("@PAGEID", PageId);

            DbHelper helper = new DbHelper();
            dtSubMenu = helper.GetDataTable(cmd, "");
            rptSubMenu.DataSource = dtSubMenu;
            rptSubMenu.DataBind();

            Session["MainPageId"] = PageId;
        }
    }
}
